/*
 * StringComparator.java
 *
 * Created on 2008/09/07 +0900
 *
 * $Revision: 60 $
 * $Date: 2008-12-28 19:27:48 +0000 (Sun, 28 Dec 2008) $
 */

package org.gikolet.framework.util;

/**
 * @author tetsutaro (latest modification by $Author: tetsutaro.ss $)
 * @version $Revision: 60 $ $Date: 2008-12-28 19:27:48 +0000 (Sun, 28 Dec 2008) $
 */
public class StringComparator implements Comparator {

    /**
     * @see org.gikolet.framework.util.Comparator#compare(java.lang.Object,
     *      java.lang.Object)
     */
    public int compare(Object o1, Object o2) {
	return compare((String) o1, (String) o2);
    }

    public int compare(String s1, String s2) {
	int len1 = s1.length();
	int len2 = s2.length();
	int n = Math.min(len1, len2);

	for (int i = 0; i < n; i++) {
	    char c1 = s1.charAt(i);
	    char c2 = s2.charAt(i);

	    if (c1 != c2) {
		return c1 - c2;
	    }
	}

	return len1 - len2;
    }
}
